package com.lingnan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lingnan.dto.InterviewDto;
import com.lingnan.dto.InterviewFeedbackRequest;
import com.lingnan.dto.InterviewRequest;
import com.lingnan.entity.po.Interview;
import com.lingnan.entity.vo.Result;
import com.lingnan.service.InterviewService;
import com.lingnan.dto.InterviewQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/interview")
public class InterviewController {

    @Autowired
    private InterviewService interviewService;

    /**
     *  PC分页多条件查询,返回dto
     */

    @PostMapping("/list")
    public Result<IPage<InterviewDto>> list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                                            @RequestBody(required = false) InterviewQueryParam queryParam) {
        IPage<InterviewDto> result = interviewService.getListByQueryParam(
                pageNum,
                pageSize,
                queryParam
        );
        return Result.ok(result);
    }

    /**
     * 创建面试安排，被远程调用
     */

    @PostMapping("create")
    public Result<Interview> createInterview(@RequestBody Interview interview) {
        Interview response = interviewService.createInterview(interview);
        return Result.ok(response);
    }


    /**
     * 根据主键id获取面试记录详情，单纯返回表
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Interview> getInterviewById(@PathVariable("id") Long id) {
        Interview response = interviewService.getInterviewById(id);
        return Result.ok(response);
    }

    /**
     * 根据hr_id获取面试记录详情，单纯返回表
     * @param id
     * @return
     */
    @GetMapping("Hr/{hrId}")
    public Result<Interview> getInterviewByHrId(@PathVariable("hrId") Long id) {

        List<Interview> response = interviewService.getInterviewByHrId(id);
        return Result.ok(response);
    }

    /**
     * 根据申请记录id获取面试记录详情
     * @param applicationId
     * @return
     */
    @GetMapping("/getInterviewsByApplicationId/{applicationId}")
    Result<List<Interview>> getInterviewsByApplicationId(@PathVariable("applicationId") Long applicationId) {
        List<Interview> interviews = interviewService.getInterviewsByApplicationId(applicationId);
        if(!interviews.isEmpty()){
            return Result.ok(interviews);
        }else {
            return Result.fail("没有找到该申请记录");
        }

    }

    /**
     * 删除某个申请记录对应的面试记录
     * @param applicationId
     * @return
     */
    @DeleteMapping("/deleteInterviewsByApplicationId/{applicationId}")
    Result deleteInterviewsByApplicationId(@PathVariable("applicationId") Long applicationId){
        Boolean b = interviewService.deleteInterviewsByApplicationId(applicationId);
        if(b){
            return Result.ok();
        }else {
            return Result.fail("没有找到该申请记录");
        }
    }

    // 删除面试安排
    @DeleteMapping("/{id}")
    public Result<Void> deleteInterview(@PathVariable("id") Long id) {
        interviewService.removeById(id);
        return Result.ok();
    }



    /**
     * 更新面试状态
     * @param request 面试信息，至少包含id和status字段
     * @return 更新结果
     */
    @PutMapping("/status")
    public Result<Void> updateInterviewStatus(@RequestBody Interview request) {
    try {

            boolean success = interviewService.updateInterviewStatus(request);

            if (success) {
                return Result.ok("面试状态更新成功");
            } else {
                return Result.fail("面试状态更新失败");
            }

        } catch (Exception e) {
            log.error("更新面试状态异常: {}", e.getMessage(), e);
            return Result.fail("系统异常，请稍后重试");
        }
    }

    /**
     * 面试官添加反馈意见
     * @param request 包含面试ID和反馈内容的请求体
     * @return 操作结果
     */
    @PutMapping("/feedback")
    public Result<Void> addInterviewerFeedback(@RequestBody InterviewFeedbackRequest request) {
        try {
            // 参数校验
            if (request.getInterviewId() == null) {
                return Result.fail("面试ID不能为空");
            }
            if (request.getFeedback() == null || request.getFeedback().trim().isEmpty()) {
                return Result.fail("反馈内容不能为空");
            }
            if (request.getFeedback().length() > 1000) {
                return Result.fail("反馈内容不能超过1000字");
            }

            boolean success = interviewService.addInterviewerFeedback(
                    request.getInterviewId(),
                    request.getFeedback()
            );

            if (success) {
                return Result.ok("反馈添加成功");
            } else {
                return Result.fail("反馈添加失败");
            }

        } catch (Exception e) {
            log.error("添加面试官反馈异常: {}", e.getMessage(), e);
            return Result.fail("系统异常，请稍后重试");
        }
    }
}
